package ru.resumeService.web.servlet;

import ru.resumeService.model.Resume;
import ru.resumeService.service.ResumeService;
import ru.resumeService.service.impl.ResumeServiceImpl;
import ru.resumeService.service.impl.UserServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class Search extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");

        ResumeService resumeService = new ResumeServiceImpl();
        String fieldName = request.getParameter("fieldname");
        String value = request.getParameter("value");
        if (fieldName.equals("status")){
            if(value.equals("работает")){
                value="1";
            }  else {
                value="0";
            }
        }
        List<Resume> resumes = resumeService.searchResume(fieldName, value);
        request.setAttribute("resumes", resumes);
        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/search.jsp");
        if (dispatcher != null) {
            try {
                dispatcher.forward(request, response);
            } catch (ServletException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }
}
